// // ******************************************************************
// //       /\ /|       @file       ServiceCollectionExtensions.cs
// //       \ V/        @brief      await29
// //       | "")       @author     Await, 285283010@qq.com
// //       /  |
// //      /  \\        @Modified   2025-02-01 16:02
// //    *(__\_\        @Copyright  Copyright (c) , 2025-02-01 10:02 Await
// // ******************************************************************

using AutomaticSubtitles.Contracts.Services;
using AutomaticSubtitles.Service.BackgroundServices;
using AutomaticSubtitles.Service.Infrastructure.Services;

namespace AutomaticSubtitles.Service.Infrastructure.Extensions;

public static class ServiceCollectionExtensions
{
    public static IServiceCollection AddTranslationServices(this IServiceCollection services)
    {
        // 注册为 Singleton 以确保整个应用程序生命周期内使用同一个实例
        services.AddSingleton<AIBackgroundService>();
        services.AddSingleton<ITranslationService>(sp => sp.GetRequiredService<AIBackgroundService>());
        services.AddHostedService(sp => sp.GetRequiredService<AIBackgroundService>());

        return services;
    }

    /// <summary>
    /// 添加字幕提取相关服务
    /// </summary>
    /// <param name="services">服务集合</param>
    /// <param name="configuration">配置</param>
    /// <returns>服务集合</returns>
    public static IServiceCollection AddSubtitleExtractionServices(this IServiceCollection services, IConfiguration configuration)
    {
        // 注册字幕提取配置
        var subtitleExtractionConfig = configuration.GetSection("SubtitleExtraction").Get<SubtitleExtractionConfig>() ?? new SubtitleExtractionConfig();
        services.AddSingleton(subtitleExtractionConfig);

        // 注册临时文件清理服务
        services.AddHostedService<TempFileCleanupService>();

        return services;
    }
}